import tkinter as tk
from tkinter import messagebox


def ButtonState(state):
    button0.config(state=state)
    button1.config(state=state)
    button2.config(state=state)
    button3.config(state=state)
    button4.config(state=state)
    button5.config(state=state)
    button6.config(state=state)
    button7.config(state=state)
    button8.config(state=state)


def check():
    board = [
        block0.get(),
        block1.get(),
        block2.get(),
        block3.get(),
        block4.get(),
        block5.get(),
        block6.get(),
        block7.get(),
        block8.get()
    ]
    if '' not in board:
        messagebox.showinfo('游戏结束', '该局平局')
        ButtonState('disabled')
    else:
        a = None
        detail = ''
        # 判定行
        for i in range(3):
            if board[i * 3] == board[i * 3 + 1] == board[i * 3 + 2] != '':
                a = True
                detail = board[i * 3]
        # 判定列
        for i in range(3):
            if board[i] == board[i + 3] == board[i + 6] != '':
                a = True
                detail = board[i]
        # 判定对角线
        if board[0] == board[4] == board[8] != '' or board[2] == board[
                4] == board[6] != '':
            a = True
            detail = board[4]
        if a is True:
            messagebox.showinfo('游戏结束', '{}玩家胜利'.format(detail))
            ButtonState('disabled')
            status.set('游戏结束')
        else:
            status.set('现在是{}玩家'.format('O' if player % 2 == 0 else 'X'))
        print(a, detail)


def fill(where):
    global player
    what = 'O' if player % 2 == 0 else 'X'
    board = [
        block0.get(),
        block1.get(),
        block2.get(),
        block3.get(),
        block4.get(),
        block5.get(),
        block6.get(),
        block7.get(),
        block8.get()
    ]
    if board[where] != '':
        messagebox.showinfo('!!!', '这个格子已经被填了……')
    else:
        if where == 0:
            block0.set(what)
        elif where == 1:
            block1.set(what)
        elif where == 2:
            block2.set(what)
        elif where == 3:
            block3.set(what)
        elif where == 4:
            block4.set(what)
        elif where == 5:
            block5.set(what)
        elif where == 6:
            block6.set(what)
        elif where == 7:
            block7.set(what)
        elif where == 8:
            block8.set(what)
        player += 1
        check()


def again():
    global player
    player = 0
    ButtonState('normal')
    block0.set('')
    block1.set('')
    block2.set('')
    block3.set('')
    block4.set('')
    block5.set('')
    block6.set('')
    block7.set('')
    block8.set('')
    status.set('现在是{}玩家'.format('O' if player % 2 == 0 else 'X'))


if __name__ == '__main__':
    root = tk.Tk()
    root.geometry('300x330+100+100')
    root.title('Tic-Tac-Toe (双人版)')
    # 变量
    player = 0
    status = tk.StringVar()
    status.set('现在是{}玩家'.format('O' if player % 2 == 0 else 'X'))
    block0 = tk.StringVar()
    block1 = tk.StringVar()
    block2 = tk.StringVar()
    block3 = tk.StringVar()
    block4 = tk.StringVar()
    block5 = tk.StringVar()
    block6 = tk.StringVar()
    block7 = tk.StringVar()
    block8 = tk.StringVar()
    # 一层控件
    tk.Label(root, text='井字棋', font=('楷体', 20)).pack(padx=4, pady=2)
    frame = tk.Frame(root)
    frame.pack(padx=4, pady=2)
    btn_again = tk.Button(root, text='重新开始', font=('楷体', 20), command=again)
    btn_again.pack(padx=4, pady=2)
    tk.Label(root, textvariable=status, font=('楷体', 15)).pack(padx=4, pady=2)
    # 二层控件
    button0 = tk.Button(frame,
                        textvariable=block0,
                        font=("Montserrat", 20, "bold"),
                        height=1,
                        width=3,
                        command=lambda: fill(0))
    button0.grid(row=0, column=0, padx=4, pady=2)
    button1 = tk.Button(frame,
                        textvariable=block1,
                        font=("Montserrat", 20, "bold"),
                        height=1,
                        width=3,
                        command=lambda: fill(1))
    button1.grid(row=0, column=1, padx=4, pady=2)
    button2 = tk.Button(frame,
                        textvariable=block2,
                        font=("Montserrat", 20, "bold"),
                        height=1,
                        width=3,
                        command=lambda: fill(2))
    button2.grid(row=0, column=2, padx=4, pady=2)
    button3 = tk.Button(frame,
                        textvariable=block3,
                        font=("Montserrat", 20, "bold"),
                        height=1,
                        width=3,
                        command=lambda: fill(3))
    button3.grid(row=1, column=0, padx=4, pady=2)
    button4 = tk.Button(frame,
                        textvariable=block4,
                        font=("Montserrat", 20, "bold"),
                        height=1,
                        width=3,
                        command=lambda: fill(4))
    button4.grid(row=1, column=1, padx=4, pady=2)
    button5 = tk.Button(frame,
                        textvariable=block5,
                        font=("Montserrat", 20, "bold"),
                        height=1,
                        width=3,
                        command=lambda: fill(5))
    button5.grid(row=1, column=2, padx=4, pady=2)
    button6 = tk.Button(frame,
                        textvariable=block6,
                        font=("Montserrat", 20, "bold"),
                        height=1,
                        width=3,
                        command=lambda: fill(6))
    button6.grid(row=2, column=0, padx=4, pady=2)
    button7 = tk.Button(frame,
                        textvariable=block7,
                        font=("Montserrat", 20, "bold"),
                        height=1,
                        width=3,
                        command=lambda: fill(7))
    button7.grid(row=2, column=1, padx=4, pady=2)
    button8 = tk.Button(frame,
                        textvariable=block8,
                        font=("Montserrat", 20, "bold"),
                        height=1,
                        width=3,
                        command=lambda: fill(8))
    button8.grid(row=2, column=2, padx=4, pady=2)
    # again()

    root.mainloop()
